#include "index.hpp"
#include "HtmlUtil.hpp"
#include "searcher.hpp"
#include "cpp-httplib/httplib.h"

const std::string input = "./raw_html/raw.txt";
const std::string root_path = "./wwwroot";


int main()
{
    // 构建索引测试对象
    ns_searcher::Searcher* p_searcher = new ns_searcher::Searcher();
    p_searcher->InitSearcher(input);

    httplib::Server srv;
    srv.set_base_dir(root_path.c_str());   // 设置网页根目录

    srv.Get("/s", [p_searcher](const httplib::Request& req, httplib::Response& resp){
        if(!req.has_param("word"))
        {
            resp.set_content("Error: 缺少搜索关键字", "text/plain; charset=utf-8");
            return;
        }

        std::string word = req.get_param_value("word");
        std::string jsionStr;
        p_searcher->Search(word, &jsionStr);
        resp.set_content(jsionStr.c_str(), "application/json");
    });

    srv.listen("0.0.0.0", 8081);

    return 0;
}
